<template>
  <!-- 新增部门的弹层 -->
  <el-dialog
    slot="title"
    :title="form.id ? '编辑部门' : '添加部门'"
    :visible="dialogVisible"
    width="80%"
    center
    @close="handleClose"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="right"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择">
          <el-option
            v-for="itm in manager"
            :key="itm.id"
            :label="itm.username"
            :value="itm.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>

    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  getDepartments,
  addDepartments,
  getUserInfoById,
  updateDeaprtInfo
} from '@/api/departments'
import { getManager } from '@/api/employees'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    clickObj: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkName = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let flag = false
      if (this.form.id) {
        flag = depts
          .filter(
            (itm) =>
              itm.id !== this.clickObj.id && itm.pid === this.clickObj.pid
          )
          .some((obj) => obj.name === value)
      } else {
        flag = depts
          .filter((itm) => itm.pid === this.clickObj.id)
          .some((obj) => obj.name === value)
      }

      if (flag) {
        return callback(new Error(`该部门下有${value}相同的子部门`))
      } else {
        return callback()
      }
    }
    const checkCode = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let flag = false
      if (this.form.id) {
        flag = depts.some(
          (itm) => itm.id !== this.clickObj.id && itm.code === value && value
        )
      } else {
        flag = depts.some((itm) => itm.code === value && value)
      }
      if (flag) {
        return callback(new Error(`组织架构中已经有部门使用${value}编码`))
      } else {
        return callback()
      }
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称必填', trigger: 'blur' },
          { min: 1, max: 50, message: '名称必须在1-50个字符', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码必填', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码必须在1-50个字符',
            trigger: 'blur'
          },
          { validator: checkCode, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门负责人必填', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '部门介绍必须在1-300个字符',
            trigger: 'blur'
          }
        ]
      },
      manager: []
    }
  },
  created() {
    // 获取负责人接口
    this.getManager()
  },
  methods: {
    // 关闭弹层
    handleClose() {
      this.$emit('cancelPopup')
      console.log(111)
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.form.resetFields()
    },
    // 新增部门提交
    submit() {
      this.$refs.form.validate(async(isOk) => {
        if (isOk) {
          if (this.form.id) {
            // 编辑部门
            await updateDeaprtInfo(this.form)
          } else {
            // 添加部门
            await addDepartments({
              ...this.form,
              pid: this.clickObj.id
            })
          }
          this.$message(this.form.id ? '更新成功' : '添加成功')
          this.$emit('cancelPopup')
        }
      })
    },
    // 获取管理员
    async getManager() {
      this.manager = await getManager()
    },
    // 根据ID获取编辑信息
    async getUserInfoById(id) {
      this.form = await getUserInfoById(id)
    }
  }
}
</script>

<style>
</style>
